package  de.cube3d.blitwit.render
{
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	import de.cube3d.blitwit.space.SpaceObject
	
	public class AnimatedBitmap extends BlitSpriteBitmap 
	{
		private var _sprites:Vector.<BitmapData>;
		public var fps:Number;
		
		public function AnimatedBitmap(sprites:Vector.<BitmapData>, x:Number, y:Number, fps:Number = 20) 
		{
			super(sprites[0], x, y);
			_sprites = sprites;
			isAnimated = true;
			this.fps = fps;
		}
		
		static private var _srcrectdict:Dictionary = new Dictionary(true);
		override public function animationTick(scene:BlitSpriteScene, t:Number, data:*):void 
		{
			var bm:BitmapData = _sprites[int((t * fps * 0.001)) % _sprites.length ];
			if (bm == _bm) return;
			_bm = bm;
			var mark:Rectangle = _srcrectdict[_bm];
			if (!mark) {
				_srcrectdict[_bm] = mark = _bm.getColorBoundsRect(0xf8000000, 0,false);
			}
			scene.markArea(mark.x+x, mark.y+y, mark.width, mark.height);
		}
		
		override public function getCopy(overwrite:SpaceObject = null):SpaceObject {
			if (!overwrite)
				return new AnimatedBitmap(_sprites, x, y, fps);
			var cp:AnimatedBitmap = super.getCopy(overwrite) as AnimatedBitmap;
			cp._sprites = _sprites;
			cp.fps = fps;
			return cp;
		}
	}

}